// cart.js
var util = require("../../utils/util.js");
var app = getApp()

Page({

  data: {

  },

  gotoIndex : function(){
    wx.switchTab({
      url: '../index/index',
    })
  },

  //全选点击事件
  allCheckbox : function(){
    var status = null;
    var thiz = this;
    var totalPrice=0;
    //如果全选选中,商品就选中
    if (!thiz.data.checked){
      status = true;
    }else{
      status = false;
    }
    for (var i = 0; i < thiz.data.cartGoodsArray.length; i++) {
        thiz.data.cartGoodsArray[i].checked = status;
        if(status){
        	totalPrice+=parseFloat(thiz.data.cartGoodsArray[i].amount) * parseFloat(thiz.data.cartGoodsArray[i].price);
        }
      }
    for (var i = 0; i < thiz.data.cartGoodsArray.length; i++) {

      if (thiz.data.editorIsClick) {
        //如果编辑点击状态
        if (thiz.data.cartGoodsArray[i].checked) {
          //如果商品是选中状态
          thiz.data.editorStatus = 'goodsSelectEditor';
          break;
        } else {
          thiz.data.editorStatus = 'editor';
        }

      } else {
        thiz.data.editorStatus = 'normal';
      }
    }
    this.setData({
    	totalPrice:totalPrice,
      editorStatus: thiz.data.editorStatus,
      checked: !this.data.checked,
      cartGoodsArray: this.data.cartGoodsArray
    })
  },

  //商品加数量点击事件
  goodsAddClick : function(e){
    var thiz = this
    var id = e.target.dataset.id;
    var goods_id = e.target.dataset.goods_id;
    thiz.data.cartGoodsArray.forEach(function(element, index){
      if (element.goods_option_id == id && element.id == goods_id){
        element.amount ++;
      }
    })

    thiz.calcPrice()

    thiz.setData({
      cartGoodsArray: thiz.data.cartGoodsArray
    })

  },
  //商品减数量点击事件
  goodsReduceClick : function(e){
    var id = e.target.dataset.id;
    var goods_id = e.target.dataset.goods_id;

    this.data.cartGoodsArray.forEach(function (element, index) {
      if (element.goods_option_id == id && element.id == goods_id) {
        if(element.amount > 1){
          element.amount--;
        }
      
      }
    })

    this.calcPrice()

    this.setData({
      cartGoodsArray: this.data.cartGoodsArray
    })

  },
  //商品选择框点击事件
  goodsCheckbox : function(e){
    var thiz = this;
    var id = e.target.dataset.id;
    var goods_id = e.target.dataset.goods_id;    
    var checked=true;

    for (var k = 0; k < thiz.data.cartGoodsArray.length; k++){
      if (thiz.data.cartGoodsArray[k].goods_option_id == id && thiz.data.cartGoodsArray[k].id == goods_id){
        thiz.data.cartGoodsArray[k].checked = !thiz.data.cartGoodsArray[k].checked
      }
    }

    for (var i = 0; i < thiz.data.cartGoodsArray.length;i++){
    	//如果存在未选中的，全选按钮设为false
        if(!thiz.data.cartGoodsArray[i].checked){
        	checked=false;
        }
    }

    for (var i = 0; i < thiz.data.cartGoodsArray.length; i++) {
     
      if (thiz.data.editorIsClick) {
        //如果编辑点击状态
        if (thiz.data.cartGoodsArray[i].checked) {
          //如果商品是选中状态
          thiz.data.editorStatus = 'goodsSelectEditor';
          break;
        } else {
          thiz.data.editorStatus = 'editor';
        }

      } else {
        thiz.data.editorStatus = 'normal';
      }

    }

    thiz.calcPrice()
    thiz.setData({
    	checked:checked,
      editorStatus: thiz.data.editorStatus,
      cartGoodsArray: this.data.cartGoodsArray
    })

  },
  //编辑的点击事件
  editorClick : function(){
    for (var i = 0; i < this.data.cartGoodsArray.length; i++) {
      if (this.data.cartGoodsArray[i].checked){
        this.data.cartGoodsArray[i].checked = !this.data.cartGoodsArray[i].checked;
      }
    }

    var editorColor = null;
    var editorStatus = null;

    this.data.editorIsClick = !this.data.editorIsClick;

    if (this.data.editorIsClick){
      editorStatus = 'editor';
    }else{
      editorStatus = 'normal';
    }

    if (this.data.editorIsClick){
      editorColor = 'rgb(178,178,178)';
    }else{
      editorColor = null;
    }

    this.setData({
      checked: false,
      editorStatus: editorStatus,
      editorColor: editorColor,
      cartGoodsArray: this.data.cartGoodsArray
    })

    
  },
  
  //取消按钮
  cancelSelect : function(){
    this.setData({
      checked: false,                     //全选是否点中
      editorColor: null,
      editorIsClick: false,               //编辑按钮是否默认点击
      editorStatus: 'normal'              //编辑模式
    })
  },

  //删除按钮
  deleteClick : function(){
    var thiz = this;
    var noGoods = true
    var array = [];
    if (thiz.data.editorStatus == 'goodsSelectEditor'){

      wx.showModal({
        title: '提示',
        content: '是否删除当前选择的购物车商品',
        showCancel: true,
        success: function(res){
          if(res.confirm){

            var cartStorage = wx.getStorageSync('cartStorage')


            for (var i = 0; i < thiz.data.cartGoodsArray.length; i++) {
              if (thiz.data.cartGoodsArray[i].checked) {
                array.push(thiz.data.cartGoodsArray[i])
              }
            }

            array.forEach(function(element, index){

              thiz.data.cartGoodsArray.forEach(function(e, cindex){
                if (e.goods_option_id == element.goods_option_id){
                  thiz.data.cartGoodsArray.splice(cindex, 1)
                }
              })
              cartStorage.forEach(function(e, cindex){
                if (e.active_goods_options.goods_option_id == element.goods_option_id){
                  cartStorage.splice(cindex, 1)
                }
              })
            })

            wx.setStorageSync('cartStorage', cartStorage)

            if (thiz.data.cartGoodsArray == 0){
              noGoods = true
            }else{
              noGoods = false
            }

            thiz.setData({
              noGoods: noGoods,
              totalPrice : 0,
              cartGoodsArray: thiz.data.cartGoodsArray,
              checked: false,                     //全选是否点中
              editorColor : null,
              editorIsClick: false,               //编辑按钮是否默认点击
              editorStatus: 'normal'              //编辑模式
            });

            wx.showToast({
              title: '删除成功',
            })
          }
        }
      })
    }

    thiz.calcPrice()
  },
  //立刻计算
  payClick : function(){
    var thiz =this
    var pay_goods=new Array();
    var no_pay_goods = new Array();    
    var isChecked = false;
    thiz.data.cartGoodsArray.forEach(function(element, index){
      if(element.checked){
        isChecked = true;
      }
    })

    if(!isChecked){
      wx.showModal({
        title: '提示',
        content: '请选择商品在结算',
        showCancel: false,
      })
    } else if (!app.globalData.inv_i) {
      wx.navigateTo({
        url: '../login/login',
      })
    } else{
     
      var cartStorage = wx.getStorageSync('cartStorage')
      thiz.data.cartGoodsArray.forEach(function (element, index) {
        cartStorage.forEach(function(e, zindex){
          if (element.goods_option_id == e.active_goods_options.goods_option_id){
            e.amount = element.amount;
          }
        })
      })
      
      //去支付的商品
      for (var i = 0; i < thiz.data.cartGoodsArray.length; i++) {
        if(thiz.data.cartGoodsArray[i].checked){
          pay_goods.push(thiz.data.cartGoodsArray[i]);
        }else{
          cartStorage.forEach(function (e, zindex) {
            if (thiz.data.cartGoodsArray[i].goods_option_id == e.active_goods_options.goods_option_id && thiz.data.cartGoodsArray[i].id == e.active_goods_options.goods_id) {
              no_pay_goods.push(e)
            }
          })
        }
      }

      wx.setStorageSync('pay_goods', pay_goods);
    
      wx.setStorageSync('no_pay_goods', no_pay_goods);


      cartStorage.forEach(function(element, index){
        element.goods_detail.goods_describe = ""
      })

      var str = JSON.stringify(cartStorage);
      var str2 = JSON.stringify(pay_goods);
      wx.navigateTo({
//        url: '../cartConfirm/cartConfirm?postGoods=' + str + '&totalPrice=' + thiz.data.totalPrice + '&type=cart',
        url: '../cartConfirm/cartConfirm?pay_goods=' + str2 + '&postGoods='+str+'&totalPrice=' + thiz.data.totalPrice + '&type=cart',
      })
    }
  },
  calcPrice : function(){
    var thiz = this
    var totalPrice = 0

    thiz.data.cartGoodsArray.forEach(function(element, index){
      if(element.checked){
    	  totalPrice += (element.price * element.amount)
      }
      
    })

    thiz.setData({
      totalPrice: totalPrice
    })
  },
  onShow : function(){

   var thiz = this

  //  var background_color = app.globalData.background_color

  //  wx.setNavigationBarColor({
  //    frontColor: '#000000',
  //    backgroundColor: background_color,
  //  })

   var domain = app.globalData.domain
   var goods_is_check = false;
    var cartStorage = wx.getStorageSync('cartStorage')
    var totalPrice = 0

    var noGoods = true

    if(cartStorage.length > 0){
      var cartGoodsArray = [];
      cartStorage.forEach(function (element, index) {
    	if(goods_is_check){
    		totalPrice += element.amount * element.active_goods_options.price;
    	}
    	else{
    		totalPrice=0;
    	}
        
        var goods = {
          id: element.goods_detail.goods_id,
          goods_option_id: element.active_goods_options.goods_option_id,
          name: element.goods_detail.goods_name,
          size: element.active_goods_options.option_title,
          price: element.active_goods_options.price,
          amount: element.amount,
          img_url: element.goods_detail.default_img,
          checked: goods_is_check,
          c_id: element.goods_detail.c_id,
        }
        cartGoodsArray.push(goods);
      })

      noGoods = false
    }else{
      noGoods = true
    }
   

    thiz.setData({
      noGoods: noGoods,
     //页面加载时显示的总价格
      totalPrice: totalPrice,
      hiddenLoading : true,
      checked : false, 
      cartGoodsArray: cartGoodsArray,
    })
  },
  onLoad: function (options) {

    //获取系统信息
    var thiz = this;

    var background_color = app.globalData.background_color

    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          hiddenLoading : false,
          checked : false,                     //全选是否点中
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth,
          editorIsClick : false,               //编辑按钮是否默认点击
          editorStatus : 'normal'              //编辑模式
        });
      }
    })

  }
})